#ifndef _UI_STATE_H_
#define _UI_STATE_H_

#include "game_state.h"
#include "input/mouse.h"
#include "input/keyboard.h"
#include "input/moga.h"
#include "ui/ui_sprite.h"
#include "background/bg_scene.h"
#include "ui/ui_widget.h"
#include "ui/ui_mover.h"
#include "transition.h"
#include <vector>

namespace camera
{
	class FbCamera;
}


namespace gameflow
{

class UIState : public gameflow::GameState, public input::MouseListener, public input::KeyboardListener, input::MOGAJoystickListener
{
public:
						UIState(	camera::FbCamera*	pCamera, const std::string& BasePanel = "", const std::string& ExecuteOnEnter = "",  const std::string& ExecuteOnLeave = "" );
	virtual				~UIState();

	/// called when the game flow manager transitions into this game state
	virtual	bool		OnEnter();

	/// called when the game flow manager transitions out of this game state
	virtual	bool		OnExit();

	/// update the game
	virtual	bool		Tick(float dt);

	/// render the game
	virtual	bool		Render();

	bool				AddPanel(const std::string& Panel);
	void				SetPanelVisible(const std::string& Name, bool Vis);
	virtual std::string	GetType() { return "ui_state"; };

	void				SetMusic(const std::string& Music);
	void				LoadPanel(const std::string& File, bool ClearAll = false);

// 	Quick accessors to help setting up UIState behaviours
	ui::UIWidget*		GetWidget(const std::string& Name);
	void				SetWidgetFunctor(const std::string& Widget, ui::UIWidget::tdgEvent Event, ui::UIWidget::WIDGET_EVENT EventID = ui::UIWidget::WE_LMB_RELEASE);
	void				SetWidgetText(const std::string& Name, const std::string& Text);
	void				SetWidgetPopup(const std::string& WidgetName, const std::string& PopupName, GameTransition::TransitionEvent		OnStart=NULL, ui::UIMover::MoverEvent		OnFinish=NULL);
	void				SetupTransition(const std::string& Widget, const std::string& ToState, const std::string& Sound = "", GameTransition::TransitionEvent		OnStart=NULL, GameTransition::TransitionEvent	OnFinish=NULL);

	// setup the nav mesh for Wiimotes
	void				AddNavElement(const std::string& Name) {	if (ui::UIWidget* pWidget = GetWidget(Name)) { m_NavList.push_back(pWidget); } };
	virtual void		DPadNav(int	DirX, int DirY, int	Triggered);

	background::BGScene&	GetBG() { return m_BG; };


	virtual void		KeyDown(input::Keyboard::KEY* pKey);
	virtual void		KeyPressed(input::Keyboard::KEY* pKey);
	virtual	void		MouseButtonPressed(input::Mouse::BUTTON_STATE* pKey, const math::Vec2& Pos);
	virtual	void		MouseButtonReleased(input::Mouse::BUTTON_STATE* pKey, const math::Vec2& Pos);
	virtual	void		MouseButtonDown(input::Mouse::BUTTON_STATE* pKey, const math::Vec2& Pos);
	virtual void		MouseMove(const math::Vec2& From, const math::Vec2& To);
	virtual void		MouseWheelMove(int WheelInfo, const math::Vec2& Pos);

	ui::UIWidget*		GetLastMouseClickWidget(ui::UIWidget* pMouseUpWidget);
	void				SetBGScale(float Scale) { m_BGScale = Scale; };

	// MOGA
	virtual void		OnMOGAKeyDownEvent(int	KeyCode){};
	virtual void		OnMOGAKeyUpEvent(int	KeyCode){};
	virtual void		OnMOGAMotionEvent(float X, float Y, float Z){};
	virtual void		OnMOGAStateEvent(int	State);

	static bool			m_Donot_look_for_bganim_lua;
	

protected:
	virtual	bool		PreRender() { return true; } ;
	virtual	bool		PostRender() { return true; } ;
	virtual	bool		PostInitUI() { return true; };
	void				RenderFade();
	camera::FbCamera*				m_pCamera;
	std::vector<ui::UIWidget*>		m_Panels;
	std::vector<std::string>		m_PanelFiles;
	std::string						m_OnEnter;
	std::string						m_OnLeave;
	std::string						m_Music;
	//ui::UISprite					m_FadeSprite;
	background::BGScene				m_BG;

	std::vector<ui::UIWidget*>				m_NavList;
	int										m_CurrentIdx;

	void DELEGATE_CALL	OnMousePressedOnWidget(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget);
	ui::UIWidget*					m_pLastMouseDownWidget;
	float							m_BGScale;

	float							m_MOGAStatusDealyDisconnect;
	float							m_MOGAStatusDealyConnect;
	bool							m_NextMOGATestKeyReady;
	bool							m_MOGALastState;
};


 } // namespace gameflow

#endif // _UI_STATE_H_